package JZ19_顺时针打印矩阵;

import java.util.ArrayList;
public class Solution {
    public ArrayList<Integer> printMatrix(int [][] matrix) {
        ArrayList<Integer> list = new ArrayList<>();

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0){
            return null;
        }
        int up = 0;
        int down = matrix.length - 1;
        int left = 0;
        int right = matrix[0].length - 1;

        while (true){
            // 最上边一行
            for (int clo = left; clo <= right; clo++){
                list.add(matrix[up][clo]);
            }
            // 向下逼近
            up++;
            // 判断是否越界
            if (up > down){
                break;
            }

            // 最右边一行
            for (int row = up; row <= down; row++){
                list.add(matrix[row][right]);
            }
            // 向坐逼近
            right--;
            // 判断是否越界
            if (left > right){
                break;
            }

            // 最下边一行
            for (int col = right; col >= left; col--){
                list.add(matrix[down][col]);
            }
            // 向上逼近
            down--;
            // 判断是否越界
            if (up > down){
                break;
            }

            // 最左边一行
            for (int row = down; row >= up; row--){
                list.add(matrix[row][left]);
            }
            // 向右逼近
            left++;
            // 判断是否越界
            if (left > right){
                break;
            }

        }
        return list;
    }
}